package smartcarpool.ui;

import android.app.PendingIntent;
import android.util.Log;
import android.widget.Toast;
import com.example.smartcarpool.R;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.content.ServiceConnection;

import android.support.v4.view.ViewPager;
import android.view.Menu;
import smartcarpool.smartspaceservice.SmartSpaceService;
import smartcarpool.smartspaceservice.TripleList;

public class MainActivity extends FragmentActivity {

    public static Context appContext;

    public final static int SS_SERVICE_TASK = 0;

    public final static String PARAM_PENDING = "pendingIntent";

	private Tab tab1;
	private Tab tab2;
	private FragmentManager fragmentManager;
	private FragmentTransaction fragmentTransaction;
	private Fragment fragment1;
	private Fragment fragment2;
	private Intent intentService;
	private SCCollectionPagerAdapter scCollectionPagerAdapter;
    private ViewPager scViewPager;
    private ServiceConnection smSpConn;
    private String HOST = "192.168.0.89";
	private int PORT = 10010;
	private String SPACE_NAME = "X";
	//private Spinner spinner_Status;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

        PendingIntent pendIntent;
        pendIntent = createPendingResult(SS_SERVICE_TASK ,getIntent(), 0);

		scViewPager = (ViewPager) findViewById(R.id.pager);
		scCollectionPagerAdapter =
                new SCCollectionPagerAdapter(
                        getSupportFragmentManager(), pendIntent);
		//fragment1 = new tab1_Fragment();
		//fragment2 = new tab2_Fragment();
		//fragmentTransaction = getSupportFragmentManager().beginTransaction();
		//fragmentTransaction.add(fragment1, "TAB1");
		//fragmentTransaction.add(fragment2, "TAB2");
		//fragmentTransaction.commit();
		scViewPager.setAdapter(scCollectionPagerAdapter);
		
		appContext = getApplicationContext();
		ActionBar actionBar = getActionBar();

		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		
	    tab1 = actionBar.newTab();
	    tab2 = actionBar.newTab();
	    
	    tab1.setText("Curent Settings");
	    tab2.setText("Route Inf.");
	
	    tab1.setTabListener(new SCTabListener(scViewPager));
	    tab2.setTabListener(new SCTabListener(scViewPager));
	    actionBar.addTab(tab1);
	    actionBar.addTab(tab2);
	                   
        scViewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        // When swiping between pages, select the
                        // corresponding tab.
                        getActionBar().setSelectedNavigationItem(position);
                    }
                });	
	    //scViewPager.setCurrentItem(1);
        //tab2_Fragment fragment_tab2 = (tab2_Fragment) fragmentManager.findFragmentByTag("android:switcher:"+R.id.pager+":1");
        //fragment_tab2.setPendingIntent(pendIntent);
        //intentService = new Intent(this, SmartSpaceService.class);
        //intentService.putExtra(PARAM_PENDING, pendIntent);

        //SmartSpaceProfile ssProfile = new SmartSpaceProfile(HOST, PORT, SPACE_NAME);
        //
        //SmartSpaceService ssService = new SmartSpaceService("SmartSpaceService", ssProfile, fragmentManager);
        //intentService.putExtra("SS_Inf", ssProfile);
        //intentService.putExtra("HOST", HOST);
        //intentService.putExtra("SPACE_NAME", SPACE_NAME);
        //intentService.putExtra("PORT", PORT);
        //startService(intentService);
        
   
	}
	
	//@Override
	//public void onPause(){
	//	super.onPause();
	//}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("SmartSpace", "RequestCode: " + requestCode + ", ResultCode: " + resultCode);
        if (requestCode ==SS_SERVICE_TASK){
            switch (resultCode){
                case SmartSpaceService.RESULT_FAILURE_JOIN:{
                    Toast.makeText(this,"Smart Space join failure. Please, check your preferences and internet connection and try again.",Toast.LENGTH_LONG).show();
                    break;
                }
                case SmartSpaceService.RESULT_SUCCESS_INSERT:{
                    fragmentManager = getSupportFragmentManager();
                    fragment2 = fragmentManager.findFragmentByTag("android:switcher:"+R.id.pager+":1");

                    Toast.makeText(this,"Information has been inserted to the smart space",Toast.LENGTH_LONG).show();
                    break;
                }
                case SmartSpaceService.RESULT_FAILURE_INSERT:{
                    Toast.makeText(this,"Insert failure. Please try again.",Toast.LENGTH_LONG).show();
                    break;
                }
                case SmartSpaceService.RESULT_SUCCESS_QUERY:{
                    fragmentManager = getSupportFragmentManager();
                    fragment2 = fragmentManager.findFragmentByTag("android:switcher:"+R.id.pager+":1");

                    Toast.makeText(this,"Information has been queried from the smart space",Toast.LENGTH_LONG).show();
                    TripleList tripleList = data.getExtras().getParcelable(SmartSpaceService.SMART_SPACE_QUERY_ANSWER);
                    Log.d("SmartSpace", "QueryResult" + tripleList.toVector().toString());
                    break;
                }
                case SmartSpaceService.RESULT_FAILURE_QUERY:{
                    Toast.makeText(this,"Query failure. Please try again.",Toast.LENGTH_LONG).show();
                    break;
                }
                case SmartSpaceService.RESULT_SUCCESS_SUBSCRIBE:{
                    fragmentManager = getSupportFragmentManager();
                    fragment2 = fragmentManager.findFragmentByTag("android:switcher:"+R.id.pager+":1");

                    Toast.makeText(this,"Information has been queried from the smart space",Toast.LENGTH_LONG).show();
                    TripleList tripleList = data.getExtras().getParcelable(SmartSpaceService.SMART_SPACE_QUERY_ANSWER);
                    Log.d("SmartSpace", "QueryResult" + tripleList.toVector().toString());
                    break;
                }
                case SmartSpaceService.RESULT_FAILURE_SUBSCRIBE:{
                    Toast.makeText(this,"Query failure. Please try again.",Toast.LENGTH_LONG).show();
                    break;
                }
            }
        }
    }

}
